package com.wx.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wx.common.PageParam;
import com.wx.common.WrapMapper;
import com.wx.common.Wrapper;
import com.wx.domain.dto.AgreementDto;
import com.wx.domain.vo.NurseAgreementVo;
import com.wx.domain.vo.PatientAgreementVo;
import com.wx.service.SysAgreementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;


/**
 * <p>Title:      SysAgreementController. </p>
 * <p>Description 范围表 </p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company:    医百科技有限公司 </p>
 *
 * @author         yangwensheng@100doc.com.cn
 * @since      2021-03-22 10:51:46
 */
@RestController
@RequestMapping(value = "/sysAgreement", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(tags = "协议管理", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class SysAgreementController {

    @Resource
    private SysAgreementService sysAgreementService;


    @PostMapping("/listPageWithPatient")
    @ApiOperation(httpMethod = "POST", value = "分页查询患者协议")
    public Wrapper<PageInfo<PatientAgreementVo>> listPageWithPatient(@ApiParam(name = "sysAgreement", value = "sysAgreement") @RequestBody PageParam<AgreementDto> pageParam) {
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<PatientAgreementVo> list = sysAgreementService.listPageWithPatient(pageParam.getParam());
        PageInfo<PatientAgreementVo> pageInfo = new PageInfo<>(list);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, pageInfo);
    }

    @PostMapping("/listPageWithNurse")
    @ApiOperation(httpMethod = "POST", value = "分页查询护士协议")
    public Wrapper<PageInfo<NurseAgreementVo>> listPageWithNurse(@ApiParam(name = "sysAgreement", value = "sysAgreement") @RequestBody PageParam<AgreementDto> pageParam) {
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<NurseAgreementVo> list = sysAgreementService.listPageWithNurse(pageParam.getParam());
        PageInfo<NurseAgreementVo> pageInfo = new PageInfo<>(list);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, pageInfo);
    }

    @PostMapping(value = "/exportWithPatient")
    @ApiOperation(httpMethod = "POST", value = "导出进行中的会议列表")
    public void exportWithPatient(HttpServletResponse response,@ApiParam(name = "sysAgreement", value = "sysAgreement") @RequestBody AgreementDto agreementDto) throws IOException {
        List<PatientAgreementVo> list = sysAgreementService.listPageWithPatient(agreementDto);
        //添加响应头信息
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("患者协议列表.xlsx", "UTF-8"));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        //获取数据
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //设置背景颜色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        //设置头字体
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 15);
        headWriteFont.setBold(true);
        headWriteCellStyle.setWriteFont(headWriteFont);
        //设置头居中
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //内容策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        //设置 水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        EasyExcel.write(response.getOutputStream(), PatientAgreementVo.class).registerWriteHandler(horizontalCellStyleStrategy)
                .sheet("患者协议列表").doWrite(list);
    }


    @PostMapping(value = "/exportWithNurse")
    @ApiOperation(httpMethod = "POST", value = "导出护士协议列表")
    public void exportWithNurse(HttpServletResponse response,@ApiParam(name = "sysAgreement", value = "sysAgreement") @RequestBody AgreementDto agreementDto) throws IOException {
        List<NurseAgreementVo> list = sysAgreementService.listPageWithNurse(agreementDto);
        //添加响应头信息
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("导出护士协议列表.xlsx", "UTF-8"));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        //获取数据
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //设置背景颜色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        //设置头字体
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 15);
        headWriteFont.setBold(true);
        headWriteCellStyle.setWriteFont(headWriteFont);
        //设置头居中
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //内容策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        //设置 水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        EasyExcel.write(response.getOutputStream(), NurseAgreementVo.class).registerWriteHandler(horizontalCellStyleStrategy)
                .sheet("导出护士协议列表").doWrite(list);
    }

}